<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      background-color: rgb(236, 175, 175);
    }
   .news .router-link-exact-active{
      background-color: rgb(130, 212, 187);
    }
  </style>
</head>

<body>
  <div id="app">
    
    <router-link to="/home">到首页</router-link>
    <router-link to="/news">到新闻页</router-link>
    <router-link to="/about" replace>到关于我们页</router-link>
   
    <!-- 路由组件出口定义在哪里 路由组件就在哪里渲染 -->
    <router-view></router-view>
  </div>
  <script src="./vue.global.js"></script>
  <script src="./vue-router.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp();
    // 1提前准备 三个路由组件的config
    const HomePage = {
      template: `
        <div>
          <h2>这是首页</h2>  
        </div>
      `
    }
    const NativeNews = {
      template: `
        <div>
          <h3>这是本土新闻</h3>  
          
        </div>
      `
    }
    const AbroadNews = {
      template: `
        <div>
          <h3>这是海外新闻</h3>  
        </div>
      `
    }
    const NewsPage = {
      template: `
        <div class="news">
          <h2>这是新闻页</h2>  
          <router-link to="/news/native">本土新闻</router-link>
           <router-link to="/news/abroad">海外新闻</router-link>
           <hr/>
          <router-view></router-view>
        </div>
      `
    }
    const AboutPage = {
      template: `
        <div>
          <h2>这是关于我们页</h2>  
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h2>哦吼404啦</h2>  
        </div>
      `
    }
    // 创建路由实例
    const routes = [
      {
        path: '/home', // 路由路径
        component: HomePage
      },
      {
        path: '/news',
        component: NewsPage,
        // 定义children属性 children属性中定义当前路由的子路由
        children: [
          {
            // 子路由path 建议携带一级路由path作为前缀
            path: '/news/native',
            component: NativeNews
          },
          {
            path: '/news/abroad',
            component: AbroadNews
          },
          {
            path: '/news',
            redirect: '/news/native'
          }
        ]
      },
      {
        path: '/about',
        component: AboutPage
      },
      {
        path: '/',
        redirect: '/home' // redirect代表重定向
      },
      {
        path: '/:pathMatch(.*)*',
        component: NotFound
      }
    ]
    const router = VueRouter.createRouter({
      // history定义路由模式 这里使用hash模式
      history: VueRouter.createWebHashHistory(),
      // 定义路由规则
      routes
    })
    app.use(router)
    app.mount('#app')
  </script>
</body>

</html>